<?php

/**
 * @author 陈韵
 * 龙潭仓创建订单
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\Shoplongtan\ShoplongtanApiClient;
use App\Service\TraitService\SupplierRouterOrderTrait;
use App\Model\CommodityModel\Stockinfo;

class Shoplongtan extends BaseSupplierRouterService
{
    use SupplierRouterOrderTrait;
    
    /**
     * 订单申报
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        //查询商品资料
        $supplier_skuids_map  = [];
        $supplierInfo = array_column($param['orderGoods'],'supplier_skuid');
        foreach ($param['orderGoods'] as $orderGoods){
            $spec_num       = $orderGoods['spec_num'];
            $where = [
                'SupplierCommodityID'   => $orderGoods['supplier_skuid'],
                'BatchNo'               => $orderGoods['batch_no'],
            ];
            $stockInfo = Stockinfo::instance()->where($where)->get()->toArray();
            $specList = json_decode($stockInfo[0]["SpecList"],true);
            $supplier_skuids_map[$orderGoods['supplier_skuid']] = array_column($specList,'SpecificationId','SpecificationNum');
        }
        $param['supplier_skuid_map'] = $supplier_skuids_map;
        return $this->callSdkApi(ShoplongtanApiClient::class,$param['apiBody'])->createOrder($param);

    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }
    
    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return $this->callSdkApi(ShoplongtanApiClient::class,$param['apiBody'])->cancelOrder($param);
    }
    
    /**
     * 获取物流信息
     * @param array $param
     */
    public function orderExpress( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 商品列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsList( $param = [] )
    {
        $res = $this->callSdkApi(ShoplongtanApiClient::class,$param['apiBody'])->queryGoodsList($param['queryBody']);
        if($res["code"] != Normal::SUC or !isset($res['data']["has_next"])){
            return $res;
        }
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'total'     => count($res['data']['data']) ?? 0,
            'list'      => $res['data']['data'],
            'has_next'   => $res['data']['has_next'] ?? false
        ]);
    }
}